package com.zqr.lock;

import java.util.concurrent.locks.ReentrantLock;

// 该类主要是用ReentrantLock来处理原子性问题
public class ReentrantLockDemo1 {
    private static int count;
    private static ReentrantLock lock = new ReentrantLock();
    public static void incr(){
        try {
            Thread.sleep(1);
            lock.lock();
            count++;
            lock.unlock();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            new Thread(ReentrantLockDemo1::incr).start();
        }

        try {
            Thread.sleep(2000);
            System.out.println(count);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
